package com.wangbaokang.duanxin.tools;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.CursorAdapter;

/**
 * Created by wangbaokang on 2016/8/19 0019.
 */
public class MyQueryHandler extends AsyncQueryHandler {
    public MyQueryHandler(ContentResolver cr) {
        super(cr);
    }

    /**
     * 当startQuery执行完成后 回调此方法
     * @param token   是startQuery方法中的第一个参数
     * @param cookie  是startQuery方法中的第二个参数
     * @param cursor  查询后的结果
     */
    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {

        if(cookie != null && cookie instanceof CursorAdapter){
            CursorAdapter adapter = (CursorAdapter) cookie;
            //给adapter设置新的cursor
            adapter.changeCursor(cursor);
        }
        if(cursorChangedListener != null){
            cursorChangedListener.onCursorChanged(token, cookie, cursor);
        }

    }

    private IOnCursorChangedListener cursorChangedListener;

    public IOnCursorChangedListener getCursorChangedListener() {
        return cursorChangedListener;
    }

    public void setOnCursorChangedListener(IOnCursorChangedListener cursorChangedListener) {
        this.cursorChangedListener = cursorChangedListener;
    }

    /**
     * 声明cursor改变时的回调监听接口
     */
    public interface IOnCursorChangedListener{

        void onCursorChanged(int token, Object cookie, Cursor cursor);
    };

}
